<?php
require('../fpdf/fpdf.php');

class PDF_MC_Table extends FPDF
{
	var $widths;
	var $aligns;
	var $nome;          // nome do relatorio
	var $cabecalho;     // cabecalho para as colunas
	var $titulo;

	//******************************************************************************************

	function SetName($nomerel) { // nomeia o relatorio
		$this->nome = $nomerel;
	}

	function SetNameDetail($nomerel_detail) { // nomeia o relatorio
		$this->nome_detail = $nomerel_detail;
	}

	function SetTitulo($tit) { // nomeia o relatorio
		$this->titulo = $tit;
	}

	function SetCabecalho($cab) { // define o cabecalho
		$this->cabecalho = $cab;
	}

	function Header() {
		//$image = imagecreatefrompng('../../app/views/images/logo_instituicao.png');
		//imagejpeg($image, 'image.jpg', 100);
		//imagedestroy($image);
		
		$this->AliasNbPages(); // Define o numero total de paginas para a macro {nb}
		$this->Image('../../app/views/images/logo_instituicao.jpg', 8, 10, 30); // importa uma imagem
		$this->SetFont('Arial', 'B', 12);
		$this->SetX(40);
		$this->Cell($this->GetStringWidth($this->nome), 10, $this->nome);
		$this->SetX(40);
		$this->Cell($this->GetStringWidth($this->nome_detail), 22, $this->nome_detail);
		$this->SetFont('Arial', '', 10);
		$this->SetX(-30);
		$this->Cell(30, 10, utf8_decode("Página: ").$this->PageNo()."/{nb}", 0, 1); // imprime página X/Total de Páginas
		$this->SetX(-10);
		$this->line(40, 18, $this->GetX(), 18); // Desenha uma linha


		// $this->Cell(20, 5, "Nº Proc", 1, 0,'C');
		//  $this->Cell(20, 5, "Depto", 1, 0,'C');
		//  $this->Cell(155, 5, "Título", 1, 0);
		//  $this->Cell(20, 5, "Situação", 1, 0,'C');
		//  $this->Cell(30, 5, "Início", 1, 0,'C');
		$this->Cell(180, 10, "", 0, 1,'C');

		//$this->SetX(20);
		$this->SetFont('Arial', 'B', 11);
		$this->Cell(190, 5, $this->titulo, 0, 1,'C');
		$this->Cell(180, 5, "", 0, 1,'C');

		//$this->SetX(40);
		$this->SetFont('Arial', 'B', 10);
		$this->Cell(20, 5, utf8_decode("Código"), 1, 0,'C');
		$this->Cell(90, 5, "Nome do Participante", 1, 0,'L');
		$this->Cell(80, 5, "Assinatura", 1, 1,'C');

		$this->SetXY(10, 45);
	}

	function Footer() { // Rodapé : imprime a hora de impressao e Copyright
		$this->SetXY(-10, -5);
		$this->line(10, $this->GetY()-2, $this->GetX(), $this->GetY()-2);
		$this->SetX(5);
		$this->SetFont('Courier', 'B', 9);
		$data = date('d/m/Y',time());
		$hora = date('H:i',time());
		//$data = strftime("%d/%m/%Y às %T");
		$this->SetXY(-10, -5);
		//$this->Cell(130, 3, "UNINGÁ - Processo Seletivo 2009 - Impresso: ".$data." ".$hora, 0, 0, 'R');
	}
	//******************************************************************************************

	function cabecalho_lista()
	{

		//Calculate the height of the row
		$nb=0;
		for($i=0;$i<count($this->cabecalho);$i++)
			$nb=max($nb,$this->NbLines($this->widths[$i],$this->cabecalho[$i]));
		$h=5*$nb;
		//Issue a page break first if needed
		$this->CheckPageBreak($h);

		$this->SetFont('Arial', 'B', 9);

		//Cabeçalho da Tabela
		//$this->Cell(10, 15, "", 0, 1);
		$x=count($this->cabecalho);
		for($i=0; $i<$x; $i++)
			{
			$txt=$this->cabecalho[$i];
			$w=$this->widths[$i];
			$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';

			//Save the current position
			$x=$this->GetX();
			$y=$this->GetY();

			//Draw the border
			$this->Rect($x,$y,$w,$h);

		//Print the text
		$this->MultiCell($w,5,$txt,0,$a);

		//Put the position to the right of the cell
		$this->SetXY($x+$w,$y);

	}
	//Go to the next line
	$this->Ln($h);

}


function SetWidths($w)
	{
	//Set the array of column widths
	$this->widths=$w;
}

function SetAligns($a)
		{
		//Set the array of column alignments
		$this->aligns=$a;
}

		function Row($data)
			{
			//Calculate the height of the row
			$nb=0;
			for($i=0;$i<count($data);$i++)
				$nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
			$h=4*$nb;
			//Issue a page break first if needed
			$this->CheckPageBreak($h);

			$this->SetFont('Arial', '', 8);

					//Draw the cells of the row
					for($i=0;$i<count($data);$i++)
					{
					$w=$this->widths[$i];
					$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'L';
					//Save the current position
					$x=$this->GetX();
					$y=$this->GetY();
					//Draw the border
					$this->Rect($x,$y,$w,$h);
					//Print the text
			$this->MultiCell($w,4,$data[$i],0,$a);
			//Put the position to the right of the cell
			$this->SetXY($x+$w,$y);
		}
		//Go to the next line
			$this->Ln($h);
		}

		function CheckPageBreak($h)
				{
				//If the height h would cause an overflow, add a new page immediately
				if($this->GetY()+$h>$this->PageBreakTrigger)
					$this->AddPage($this->CurOrientation);
		}

		function NbLines($w,$txt)
{
//Computes the number of lines a MultiCell of width w will take
$cw=&$this->CurrentFont['cw'];
if($w==0)
	$w=$this->w-$this->rMargin-$this->x;
	$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
	$s=str_replace("\r",'',$txt);
	$nb=strlen($s);
	if($nb>0 and $s[$nb-1]=="\n")
		$nb--;
		$sep=-1;
			$i=0;
			$j=0;
			$l=0;
			$nl=1;
			while($i<$nb)
			{
				$c=$s[$i];
				if($c=="\n")
				{
				$i++;
				$sep=-1;
				$j=$i;
					$l=0;
					$nl++;
						continue;
					}
					if($c==' ')
						$sep=$i;
					$l+=$cw[$c];
					if($l>$wmax)
					{
					if($sep==-1)
					{
						if($i==$j)
					$i++;
			}
			else
				$i=$sep+1;
			$sep=-1;
			$j=$i;
			$l=0;
			$nl++;
		}
		else
			$i++;
	}
	return $nl;
}
}
?>
